/**
 * 将获取的国际事件转化为YYYY-MM-DD hh:mm:ss
 * @param date 传进来的日期
 * @param rule 想要的形式 如YYYY-MM-DD YYYY-MM-DD hh:mm:ss day(回复周几), date(今天几号)
 *              week-date(一周的号数)
 **/

// 将getDay()的转换成一二三四...日
function getDay(day) {
	switch (day) {
		case 0:
			return { key: '7', value: '周日' };
			break;
		case 1:
			return { key: '1', value: '周一' };
			break;
		case 2:
			return { key: '2', value: '周二' };
			break;
		case 3:
			return { key: '3', value: '周三' };
			break;
		case 4:
			return { key: '4', value: '周四' };
			break;
		case 5:
			return { key: '5', value: '周五' };
			break;
		case 6:
			return { key: '6', value: '周六' };
			break;
	}
}

// 将获取到的时间假如不是两位数的话在前面加0
function turnDouble(num) {
	// 假如长度为2
	if (num >= 10) {
		return num
	} else { // 否则在前面加0
		return parseInt('0' + num)
	}
}

// 获取从今天开始的一周的号数
function getWeekDate() {
  let currentDate = new Date();
  let dateList = [];

  for (let i = 0; i < 3; i++) {
    let year = currentDate.getFullYear(); // 获取年份
    let month = currentDate.getMonth() + 1; // 获取月份
    let date = currentDate.getDate(); // 获取日期
    let day = getDay(currentDate.getDay()); // 获取周几
    let hour = currentDate.getHours(); // 获取小时
    let minute = currentDate.getMinutes(); // 获取分钟
    let second = currentDate.getSeconds(); // 获取秒数

    dateList.push({
      year: year,
      month: turnDouble(month),
      date: turnDouble(date),
      day: day,
      hour: turnDouble(hour),
      minute: turnDouble(minute),
      second: turnDouble(second)
    });

    currentDate.setDate(currentDate.getDate() + 1);
  }

  return dateList;
}

export function formDate(date, rule) {
	let year = date.getFullYear() // 获取年份
	let month = date.getMonth() + 1 // 获取月份
	let today = date.getDate() // 获取今天是几号
	let day = date.getDay() // 获取周几
	let hour = date.getHours() // 获取hour
	let minute = date.getMinutes() // 获取分钟
	let second = date.getSeconds() // 获取秒数
	switch (rule) {
		case 'YYYY-MM-DD':
			return year + '-' + turnDouble(month) + '-' + turnDouble(today);
			break;
		case 'YYYY-MM-DD hh:mm:ss':
			return year + '-' + turnDouble(month) + '-' + turnDouble(today) +
				' ' + turnDouble(hour) + ':' + turnDouble(minute) + ':' + turnDouble(second);
			break;
		case 'day':
			return getDay(day);
			break;
		case 'date':
			return turnDouble(today);
			break;
		case 'week-date':
			return getWeekDate();
			break;
	}
}